import os

from core.JZ_part1 import QJZ
import execjs


def vimFile():
    jsfiile = ''
    with open("core/RSA_NJCIT_CAS.js", 'r', encoding='utf-8') as f:
        jsfiile = f.read()
        f.close()
    js = execjs.compile(jsfiile)
    with open('inf_list.txt', 'a') as f:
        while (1):
            print('请输入学号和密码，当在学号处输入0时，退出编辑')
            print('请输入学号')
            username = input()
            password = ''
            if username == '0':
                break
            else:
                print('请输入密码')
                passwd = input()
                password = js.call('sign', passwd)
            f.write(username + ' ' + password + '\n')
        print('信息保存成功')
        f.close()


def runQJZ():
    if not os.path.exists('inf_list.txt'):
        print('未检测到学号密码列表文件,请创建后再运行')
        exit(-1)
    else:
        print('检测到学号密码列表文件，正在读取中……')
        usernames = []
        passwords = []
        with open('inf_list.txt', 'r') as f:
            info = f.read()
            info1 = info.split('\n')
            for i in info1:
                if i != '':
                    info2 = i.split(' ')
                    usernames.append(info2[0])
                    passwords.append(info2[-1])
                else:
                    continue
            f.close()
        thread1 = QJZ(usernames, passwords)
        thread1.start()
        thread1.join()


def GUI():
    print('-' * 50 + '南京信息职业技术学院抢讲座系统' + '-' * 50)
    print('请输入编号：')
    print('1.编辑学号密码列表文件')
    print('2.运行抢讲座程序')
    index = input()
    if index == '1':
        vimFile()
    elif index == '2':
        runQJZ()
    else:
        print('输入错误')
        exit(-1)


if __name__ == '__main__':
    GUI()
